package cn.kai.process.chain;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 带有入参的责任链
 Author: cKnight
 Date: 2024/9/26
 */
public class ConsumerChain<T> extends AbstractChain<Consumer<T>>{

    private T data;

    public ConsumerChain(List<Consumer<T>> elements, T data) {
        super(elements);
        this.data = data;
    }

    public static <T> ConsumerChain<T> start(T data){
        return new ConsumerChain<>(new ArrayList<>(),data);
    }

    public ConsumerChain<T> next(Consumer<T> consumer){
        getElements().add(consumer);
        return this;
    }

    @Override
    public void end() {
        getElements().forEach(obj->obj.accept(data));
    }
}
